﻿using System;
using System.Drawing;
using System.Windows.Forms;
using TasksDataXml.Data;
using TasksDataXml.Exceptions;

namespace DropBoxToDo
{
    public partial class MainForm
    {
        #region Handlers
        private void OnExitClick(object sender, EventArgs e)
        {
            CommonException ex;
            _controller.SaveData(out ex);

            Close();
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            CommonException ex;
            _controller.SaveData(out ex);
        }

        private void OnNewCategoryClick(object sender, EventArgs e)
        {
            if (_categories == null)
                _categories = new CategoryContainer();

            AddCategoryNode();
        }

        private void OnAfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Label))
                return;

            var cat = e.Node.Tag as Category;
            if (cat != null)
            {
                cat.Name = e.Label;
                CommonException ex;
                _controller.SaveData(out ex);
            }

            e.Node.Text = e.Label;

            treeCategories.Invalidate();
        }

        private void TreeCategoriesOnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                return;
            var node = treeCategories.GetNodeAt(e.X, e.Y);
            if (node == null)
            {
                treeCategories.SelectedNode = null;
            }
        }

        private void OnDoubleClick(object sender, EventArgs e)
        {
            if (e is MouseEventArgs && ((MouseEventArgs)e).Button == MouseButtons.Right)
            {
                return;
            }
            
            treeCategories.ExpandAll();
        }

        private void TreeCategoriesOnNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null && e.Button == MouseButtons.Right)
            {
                string txt = string.Format("Do you want to delete [{0}] category?", e.Node.Text);
                var res = MessageBox.Show(this, txt, "Delete", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                if (res == DialogResult.Yes)
                {
                    DeleteNode(e.Node);
                }
            }
        }

        private void OnTreeCategoriesDragDrop(object sender, DragEventArgs e)
        {
            var s = sender as TreeView;
            if (s == null)
                return;

            var node = GetNodeByPoint(new Point(e.X, e.Y), s);
            if (node == null)
                return;

            var cat = node.Tag as Category;
            if(cat == null)
                return;

            var task = e.Data.GetData(typeof(Task)) as Task;
            if(task != null)
            {
                task.NotifyParentMove(task);
                cat.AddTask(task);

                CommonException ex;
                _controller.SaveData(out ex);

                LoadTasksView();
            }
        }

        private void OnTreeCategoriesDragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            var s = sender as TreeView;
            if (s == null)
                return;

            var node = GetNodeByPoint(new Point(e.X, e.Y), s);

            if (node != null)
                node.Expand();
        }

        private void OnTreeCategoriesAfterSelect(object sender, TreeViewEventArgs e)
        {
            LoadTasksView();
        }

        private void OnTasksPanelDragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void OnTasksPanelDragDrop(object sender, DragEventArgs e)
        {
            //copy task
        }


        #endregion
    }
}
